﻿using System;
using System.IO.IsolatedStorage;

namespace Prometheus.UIUtility.Storage.IsolatedStorage
{
    public class IsolatedStorageProperty<T> : StorageProperty<T>
    {
        private readonly bool _autoSave;

        public IsolatedStorageProperty(string name,bool autoSave=false, bool singletonLock = true, Func<T> lazyLoad=null) : base(name, singletonLock, lazyLoad)
        {
            _autoSave = autoSave;
        }

        protected override bool IsPropertyExists(string name)
        {
            return IsolatedStorageSettings.ApplicationSettings.Contains(name);
        }

        protected override T GetValue(string name)
        {
            return (T) IsolatedStorageSettings.ApplicationSettings[name];
        }

        protected override void SetValue(string name, T value)
        {
            IsolatedStorageSettings.ApplicationSettings[name] = value;
            if(_autoSave)
            {
                IsolatedStorageSettings.ApplicationSettings.Save();
            }
        }
    }
}